//
//  MeViewController.m
//  BTCoin
//
//  Created by Robin on 13/03/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "MeViewController.h"
#import "GFNavigationController.h"
#import "CommonCellGroup.h"
#import "CommonCellLabelItem.h"
#import "CommonCellArrowItem.h"
#import "CommonCellItem.h"
#import "MeCommonCellDataSource.h"
#import "LeftMenuHeaderView.h"
#import "LoginViewController.h"
#import "UserService.h"

@interface MeViewController ()<LeftMenuHeaderViewDelegate>
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *groupArray;
@property(nonatomic, strong) MeCommonCellDataSource *meCommonCellDataSource;
@property (nonatomic, strong) LeftMenuHeaderView *leftMenuHeaderView;
@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setHidden:YES];
    self.view.backgroundColor = COLOR_Bg_White;
    
    _meCommonCellDataSource = [[MeCommonCellDataSource alloc] init];
    [self setupGroup];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight - TabBarHeight) style:UITableViewStyleGrouped];
    _tableView.delegate = _meCommonCellDataSource;
    _tableView.dataSource = _meCommonCellDataSource;
    _tableView.sectionFooterHeight = 0;
    _tableView.sectionHeaderHeight = 10;
    _tableView.tableHeaderView = [self tableHeaderView];
    _tableView.backgroundColor = COLOR_Bg_Gay;
//    [_tableView setSeparatorColor:COLOR_Line_Small_Gay];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView reloadData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listenToLogin:) name:LISTENTOAPPLOGINNOTIFICATION object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 初始化各组数据
- (void)setupGroup
{
    [_groupArray removeAllObjects];
    
    [self setupGroup0];
    
    // 重新给数据源赋值，用于tableView reloadData操作
    _meCommonCellDataSource.groupArray = _groupArray;
}

- (void)setupGroup0 {
    @WeakObj(self);
    
    CommonCellGroup *group = [CommonCellGroup group];
    [self.groupArray addObject:group];
    
    CommonCellArrowItem *subscribe = [CommonCellArrowItem itemWithTitle:@"身份认证"];
    subscribe.icon = @"account_id_verification";
    subscribe.operation = ^(){
        
        if (KUserSingleton.bIsLogin) {
            
//                VideoColletionViewController *vc = [[VideoColletionViewController alloc]initWithType:VideoCollectionVCType_Col dataList:@[]];
//                vc.isEdit = YES;
//                [selfWeak.navigationController pushViewController:vc animated:YES];
        } else {
            
//                [AlertFactory createLoginAlert:self withMsg:@"查看订阅" block:^{
//
//                }];
        }
    };
    
    CommonCellArrowItem *chargeItem = [CommonCellArrowItem itemWithTitle:@"订单管理"];
    chargeItem.icon = @"account_order_manager";
    chargeItem.operation = ^(){
        if ([UserInfo sharedUserInfo].bIsLogin) {
//                AccountInfoViewController *selectView = [[AccountInfoViewController alloc] init];
//                [selfWeak.navigationController pushViewController:selectView animated:YES];
        }
        else
        {
//                [AlertFactory createLoginAlert:self withMsg:@"充值" block:^{
//                }];
        }
    };
    
    CommonCellArrowItem *phone = [CommonCellArrowItem itemWithTitle:@"个人中心"];
    phone.icon = @"account_user_center";
    phone.operation = ^(){
//            HelpServiceViewController * helpVC = [[HelpServiceViewController alloc] initWithNibName:nil bundle:nil];
//            [selfWeak.navigationController pushViewController:helpVC animated:YES];
    };
    
    CommonCellArrowItem *draftClass = [CommonCellArrowItem itemWithTitle:@"安全中心"];
    draftClass.icon = @"account_security_center";
    draftClass.operation = ^(){
//            DraftClassViewController * classVC = [[DraftClassViewController alloc] initWithNibName:nil bundle:nil];
//            [selfWeak.navigationController pushViewController:classVC animated:YES];
    };
    
    CommonCellArrowItem *officalNotice = [CommonCellArrowItem itemWithTitle:@"提币地址"];
    officalNotice.icon = @"account_address_manager";
    officalNotice.operation = ^(){
//            OfficalNoticeViewController * noticeVC = [[OfficalNoticeViewController alloc] initWithNibName:nil bundle:nil];
//            [selfWeak.navigationController pushViewController:noticeVC animated:YES];
    };
    
    CommonCellArrowItem * riskNotice = [CommonCellArrowItem itemWithTitle:@"设置"];
    riskNotice.icon = @"account_setting";
    riskNotice.operation = ^(){
//            InvestRiskTipViewController * noticeVC = [[InvestRiskTipViewController alloc] initWithNibName:nil bundle:nil];
//            [selfWeak.navigationController pushViewController:noticeVC animated:YES];
    };
    
    CommonCellArrowItem * aboutUs = [CommonCellArrowItem itemWithTitle:@"关于我们"];
    aboutUs.icon = @"account_about";
    aboutUs.operation = ^(){
        //            InvestRiskTipViewController * noticeVC = [[InvestRiskTipViewController alloc] initWithNibName:nil bundle:nil];
        //            [selfWeak.navigationController pushViewController:noticeVC animated:YES];
    };
    
    group.items = @[subscribe,chargeItem,phone,draftClass,officalNotice,riskNotice,aboutUs];
}

#pragma mark - event actions
- (void)listenToLogin:(NSNotification *)sender {
    BOOL login = sender.object;
    self.leftMenuHeaderView.login = login;
}


#pragma mark - leftMenuHeaderViewDelegate
- (void)enterLogin
{
    @WeakObj(self)
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    GFNavigationController * loginNav = [[GFNavigationController alloc] initWithRootViewController:loginVC];
    [self presentViewController:loginNav animated:YES completion:nil];
}

#pragma mark - private methods
- (void)getSecurityInfo {
    Loading_Pig_Show(self.view);
    [[UserService sharedUserService] getSecurity:^(id responseObject) {
        Loading_Hide(self.view);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            BaseModel * userInfo = [[BaseModel alloc] initWithDictionary:responseObject error:nil];
            
            [[UserInfo sharedUserInfo] updateUserSecurityInfo:responseObject];
            if (SuccessCode == userInfo.code) {
                [PopupViewHUD showSuccessPopup:userInfo.msg];
            } else {
                [PopupViewHUD showErrorPopup:userInfo.msg];
            }
        }
    } fail:^(NSError *error) {
        Loading_Hide(self.view);
    }];
}

#pragma mark - 懒加载
- (NSMutableArray *)groupArray
{
    if (!_groupArray) {
        _groupArray = [NSMutableArray array];
    }
    return _groupArray;
}

- (UIView *)tableHeaderView
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,kScreenWidth, 200.0)];
    headerView.backgroundColor = COLOR_Bg_Gay;
    
    _leftMenuHeaderView = [[LeftMenuHeaderView alloc] initWithFrame:CGRectMake(0, 0,headerView.width, headerView.height)];
    _leftMenuHeaderView.delegate = self;
    [headerView addSubview:_leftMenuHeaderView];
    
    return headerView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
